import { defineStore } from 'pinia';
const useUserStore = defineStore('user', {
    state() {
        return {
            userList: [
                {
                    id:1,
                    name:'杰克',
                    age:20
                },
                {
                    id:2,
                    name:'肉丝',
                    age:22
                }
            ]
        }
    },
    getters: {

        userCount(state) {
            return state.userList.length;
        }
    },
    actions: {
        // 添加用户
        userAdd(state, user) {
            const len = state.userList.length;
            const id = len > 0 ? state.userList[len - 1].id + 1 : 1;
            state.userList.push({ id, ...user });
        },
        // 删除用户
        userDel(id) {
            const index = this.userList.findIndex(item => item.id === id);
            this.userList.splice(index, 1);
        }
    }
});

export default useUserStore;